package com.sword.service.impl.sale;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.sword.constant.MessageConstant;
import com.sword.page.PageResult;
import com.sword.page.QueryPageBean;
import com.sword.pojo.sale.SaleDetail;
import com.sword.service.impl.BasicBaseServiceImpl;
import com.sword.service.sale.SaleDetailService;
import org.apache.shiro.SecurityUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.lang.reflect.Field;
@Service
@Transactional
public class SaleDetailServiceImpl extends BasicBaseServiceImpl<SaleDetail> implements SaleDetailService {
    @Override
    public PageResult querySaleDetail(QueryPageBean queryPageBean) {
        QueryWrapper<SaleDetail> wrapper = new QueryWrapper<>();
        String queryString = queryPageBean.getQueryString();
        // 如果queryString有值
        if (queryString != null) {
            SaleDetail saleDetail = new SaleDetail();
            Field[] fields = saleDetail.getClass().getDeclaredFields();
            for (int i = 0; i < fields.length; i++) {
                //有的字段是用private修饰的 将他设置为可读
                fields[i].setAccessible(true);
                if(fields[i].getName().endsWith("Date")){
                    continue;
                }
                wrapper.like(fields[i].getName(),queryString).or();
            }
        }
        IPage<SaleDetail> iPage = super.queryPageList(wrapper, queryPageBean.getPage(), queryPageBean.getLimit());
        return new PageResult(0, MessageConstant.QUERY_SALEDETAIL_SUCCESS, iPage.getTotal(), iPage.getRecords());
    }

    @Override
    public Integer addSaleDetail(SaleDetail saleDetail) {
        return super.save(saleDetail);
    }

    @Override
    public Integer editSaleDetail(SaleDetail saleDetail) {
        return super.update(saleDetail);
    }

    @Override
    public Integer examineSaleDetail(SaleDetail saleDetail) {
        String userId = (String) SecurityUtils.getSubject().getSession().getAttribute("loginUser");
        saleDetail.setExamineid(userId);
        saleDetail.setStatus(1);
        return super.update(saleDetail);
    }

    @Override
    public Integer unexamineSaleDetail(SaleDetail saleDetail) {
        saleDetail.setExamineid("");
        saleDetail.setStatus(0);
        return super.update(saleDetail);
    }

    @Override
    public Integer deleteSaleDetail(Integer id) {
        return super.deleteById(Long.valueOf(id));
    }
}
